package com.bignerdranch.android.ecar.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ListView;

import com.android.volley.Response;
import com.bignerdranch.android.ecar.Adapter.BlogAdapter;
import com.bignerdranch.android.ecar.R;
import com.bignerdranch.android.ecar.Utils.RequestUtil;
import com.bignerdranch.android.ecar.Utils.URLBank;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

public class BlogActivity extends BaseActivity implements View.OnClickListener {

    private ImageButton blog_back;
    private ImageButton edit_back;
    private ListView list_blog;
    private BlogAdapter mAdapter;
    private JSONArray mData;
    private String url;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_blog);
        initView();
        initData();
    }

    private void initView() {
        blog_back = (ImageButton) findViewById(R.id.blog_back);
        edit_back = (ImageButton) findViewById(R.id.edit_back);

        blog_back.setOnClickListener(this);
        edit_back.setOnClickListener(this);
        list_blog = (ListView) findViewById(R.id.list_blog);


    }

    private void initData(){
        url = URLBank.teacher_url+URLBank.queryByUser_url;
        Log.e("------",url);
        RequestUtil.executeJSONObject(this, url, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject o) {
                try {
                    String code = o.getString("code");
                    if (code.equals("0")){
                        mData = o.getJSONArray("data");
                        mAdapter = new BlogAdapter(getApplicationContext(),mData);
                        list_blog.setAdapter(mAdapter);
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        });
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.blog_back:
                finish();
                break;
            case R.id.edit_back:
                Intent intent = new Intent(this, BlogEditActivity.class);
                startActivity(intent);
                break;
        }
    }
}
